package gui;

import application.IAppContext;
import gui.OpenDialog;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import reader.CustomFileReader;
import writer.CustomFileWriter;

/**
 *
 * @author  Administrator
 */
public class MainWindow extends javax.swing.JFrame implements IMainWindow,IStatusPanelListener{

    private IAppContext appContext;
    private InstructionsPanel instructionsPanel;
    private JPanel combinePanel;
    private StatusPanel statusPanel;
    private DrawingPanel drawingPanel;

    /** Creates new form MainWindow */
    public MainWindow(IAppContext appContext) {
        this.appContext = appContext;
        initComponents();
        ImageIcon imageIcon = new ImageIcon(getClass().getResource("/images/windowTitleIcon.png"));
        setIconImage(imageIcon.getImage());

        //set panels
        instructionsPanel = new InstructionsPanel(appContext);
        combinePanel = new JPanel();
        combinePanel.setLayout(new BorderLayout());
        statusPanel=new StatusPanel(appContext);
        statusPanel.addStatusPanelListener(this);
        drawingPanel=new DrawingPanel(appContext);
        combinePanel.add(statusPanel,BorderLayout.NORTH);
        combinePanel.add(drawingPanel,BorderLayout.CENTER);
        splitPane.setTopComponent(instructionsPanel);
        splitPane.setBottomComponent(combinePanel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        toolBar = new javax.swing.JToolBar();
        newButton = new javax.swing.JButton();
        openButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        saveAsButton = new javax.swing.JButton();
        compileButton = new javax.swing.JButton();
        drawToolBarButton = new javax.swing.JButton();
        splitPane = new javax.swing.JSplitPane();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        toolsMenu = new javax.swing.JMenu();
        compileMenuItem = new javax.swing.JMenuItem();
        drawMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sequence Manager");

        toolBar.setRollover(true);

        newButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/new22.png"))); // NOI18N
        newButton.setToolTipText("New");
        newButton.setFocusable(false);
        newButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        newButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(newButton);

        openButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/openmaptemplate22.png"))); // NOI18N
        openButton.setToolTipText("Open");
        openButton.setFocusable(false);
        openButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        openButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });
        toolBar.add(openButton);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/filesave22.PNG"))); // NOI18N
        saveButton.setToolTipText("Save");
        saveButton.setFocusable(false);
        saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        toolBar.add(saveButton);

        saveAsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/saveAs22.PNG"))); // NOI18N
        saveAsButton.setToolTipText("Save As");
        saveAsButton.setFocusable(false);
        saveAsButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveAsButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(saveAsButton);

        compileButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/compile.png"))); // NOI18N
        compileButton.setToolTipText("Compile");
        compileButton.setFocusable(false);
        compileButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        compileButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(compileButton);

        drawToolBarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/draw.png"))); // NOI18N
        drawToolBarButton.setToolTipText("Draw");
        drawToolBarButton.setFocusable(false);
        drawToolBarButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        drawToolBarButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBar.add(drawToolBarButton);

        getContentPane().add(toolBar, java.awt.BorderLayout.PAGE_START);

        splitPane.setDividerLocation(200);
        splitPane.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        getContentPane().add(splitPane, java.awt.BorderLayout.CENTER);

        fileMenu.setMnemonic('F');
        fileMenu.setText("File");
        fileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/new_1.png"))); // NOI18N
        newMenuItem.setText("New");
        fileMenu.add(newMenuItem);

        openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/openmaptemplate22.png"))); // NOI18N
        openMenuItem.setText("Open");
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(openMenuItem);

        saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/filesave16.PNG"))); // NOI18N
        saveMenuItem.setText("Save");
        saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        saveAsMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/saveAs16.PNG"))); // NOI18N
        saveAsMenuItem.setText("Save As");
        fileMenu.add(saveAsMenuItem);

        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK));
        exitMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit22.png"))); // NOI18N
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        toolsMenu.setMnemonic('T');
        toolsMenu.setText("Tools");

        compileMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK));
        compileMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/compile24.PNG"))); // NOI18N
        compileMenuItem.setText("Compile");
        toolsMenu.add(compileMenuItem);

        drawMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.ALT_MASK));
        drawMenuItem.setText("Draw");
        toolsMenu.add(drawMenuItem);

        menuBar.add(toolsMenu);

        helpMenu.setMnemonic('H');
        helpMenu.setText("Help");

        aboutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-715)/2, (screenSize.height-613)/2, 715, 613);
    }// </editor-fold>//GEN-END:initComponents

private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
    try {
            showOpenFileDialog();//GEN-LAST:event_openButtonActionPerformed
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
    try {
        showOpenFileDialog();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
    }

}//GEN-LAST:event_openMenuItemActionPerformed
    private void showOpenFileDialog() throws FileNotFoundException, IOException, ClassNotFoundException {
        OpenDialog openDialog = new OpenDialog();
////    openDialog.SetFileType();
        JDialog.setDefaultLookAndFeelDecorated(true);
        openDialog.setAcceptAllFileFilterUsed(true);
        if (openDialog.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        }
        File file = openDialog.getSelectedFile();
        System.out.println("file obj =" + file);
        System.out.println("File path =" + file.getPath());
        CustomFileReader customFileReader = new CustomFileReader(file);
        customFileReader.readObject();
        System.out.println("file=" + customFileReader.getfileObject().toString());
//        codeTextArea.setText(customFileReader.getfileObject().toString().trim());
    }

private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuActionPerformed
}//GEN-LAST:event_fileMenuActionPerformed

private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
    showSaveFileDialog();
}//GEN-LAST:event_saveMenuItemActionPerformed

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
    showSaveFileDialog();
}//GEN-LAST:event_saveButtonActionPerformed

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
    System.exit(0);
// TODO add your handling code here:
}//GEN-LAST:event_exitMenuItemActionPerformed

    private void showSaveFileDialog() {
        try {
            String instruction = instructionsPanel.getInstructions();

            if (instruction.length() > 0) {
                SaveDialog saveDialog = new SaveDialog();
                ChooseFileTypeDialog chooseFileTypeDialog = new ChooseFileTypeDialog();
                saveDialog.setAccessory(chooseFileTypeDialog);
                saveDialog.SetDialog();
                JDialog.setDefaultLookAndFeelDecorated(true);
                saveDialog.setAcceptAllFileFilterUsed(true);
                if (saveDialog.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                }
                String string = instructionsPanel.getInstructions();

                CustomFileWriter fileWriter = new CustomFileWriter(saveDialog.getSelectedFile());
                fileWriter.writeObject(string);
            } else {
                JDialog.setDefaultLookAndFeelDecorated(true);
                JOptionPane.showMessageDialog(this, "Text Area Is Empty");
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                setDefaultLookAndFeelDecorated(true);
                new MainWindow(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JButton compileButton;
    private javax.swing.JMenuItem compileMenuItem;
    private javax.swing.JMenuItem drawMenuItem;
    private javax.swing.JButton drawToolBarButton;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton newButton;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JButton openButton;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JButton saveAsButton;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JButton saveButton;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JSplitPane splitPane;
    private javax.swing.JToolBar toolBar;
    private javax.swing.JMenu toolsMenu;
    // End of variables declaration//GEN-END:variables

    @Override
    public void drawButtonActionPerformed(ActionEvent actionEvent) {
        String instructions = instructionsPanel.getInstructions();
        drawingPanel.draw(instructions);
    }

    @Override
    public InstructionsPanel getInstructionsPanel() {
        return instructionsPanel;
    }

    @Override
    public StatusPanel getStatusPanel() {
        return statusPanel;
    }

    @Override
    public DrawingPanel getDrawingPanel() {
        return drawingPanel;
    }
}
